package com.kakaxi.mybilibili.module.home.live.view;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.adapter.LiveRoomGiftTopRecyAdapter;
import com.kakaxi.mybilibili.base.BaseFragment;
import com.kakaxi.mybilibili.module.home.live.presenter.LiveRoomGiftTopPresenter;
import com.kakaxi.mybilibili.bean.LiveRoomGiftTopItemBean;
import com.kakaxi.mybilibili.utils.NetworkCheckUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 直播间礼物排行版碎片
 * Created by kakaxi on 16/11/8.
 */

public class LiveRoomGiftTopFragment extends BaseFragment implements ILiveRoomGiftTopView {

    /** 刷新控件 **/
    @BindView(R.id.srl_common_fragment)
    SwipeRefreshLayout mGiftTopRefresh;

    /** 数据列表 **/
    @BindView(R.id.recv_common_fragment)
    RecyclerView mGiftTopRecyView;

    /** 礼物排行榜列表适配器 **/
    private LiveRoomGiftTopRecyAdapter mLiveRoomGiftTopAdapter;

    /** 控制层 **/
    private LiveRoomGiftTopPresenter mPresenter;

    public static LiveRoomGiftTopFragment newInstance() {
        LiveRoomGiftTopFragment liveRoomGiftTopFragment = new LiveRoomGiftTopFragment();
        return liveRoomGiftTopFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_common_recycler;
    }

    @Override
    protected void setupViews(ViewGroup viewGroup) {
        ButterKnife.bind(this, viewGroup);
        // 初始化列表
        mGiftTopRecyView.setLayoutManager(new LinearLayoutManager(getContext()));
        // 初始化刷新控件
        mGiftTopRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refreshGiftTopData(0);
            }
        });
    }

    @Override
    protected void initData() {
        mPresenter = new LiveRoomGiftTopPresenter(this);
        mPresenter.initGiftTopData(0);
    }

    private ArrayList<LiveRoomGiftTopItemBean> getData() {
        ArrayList<LiveRoomGiftTopItemBean> list = new ArrayList<>();
        list.add(new LiveRoomGiftTopItemBean());
        list.add(new LiveRoomGiftTopItemBean());
        list.add(new LiveRoomGiftTopItemBean());
        list.add(new LiveRoomGiftTopItemBean());
        list.add(new LiveRoomGiftTopItemBean());
        return list;
    }

    @Override
    public void showGiftTopData(List<LiveRoomGiftTopItemBean> giftTopItemBeanList) {
        if (mLiveRoomGiftTopAdapter == null) {
            mLiveRoomGiftTopAdapter = new LiveRoomGiftTopRecyAdapter(giftTopItemBeanList);
            mGiftTopRecyView.setAdapter(mLiveRoomGiftTopAdapter);
        } else {
            mLiveRoomGiftTopAdapter.setNewData(giftTopItemBeanList);
        }
    }

    @Override
    public boolean isConnectNetwork() {
        return NetworkCheckUtil.isNetworkConnected(getContext());
    }

    @Override
    public int getConnectType() {
        return 0;
    }

    @Override
    public void showLoading() {
        mGiftTopRefresh.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mGiftTopRefresh.setRefreshing(false);
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showMessage(String message) {
        toastHandler(message);
    }
}
